package com.youlu.campus.web.file;

import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.QiniuFileRecord;
import com.youlu.campus.service.qiniu.QiniuFileRecordService;
import com.youlu.campus.web.common.utils.IpUtils;
import com.youlu.campus.web.qq.QQService;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class FileUploadController {

    @Autowired
    QQService qqService;

    @Autowired
    private FileUploadService fileUploadService;


    @Autowired
    private QiniuFileRecordService qiniuFileRecordService;

    /**
     * 文件上传上传
     *
     * @param file
     * @param request
     * @return
     */
    @PostMapping(value = "/public/upload-file")
    public QResult updateFile(MultipartFile file, HttpServletRequest request) {
        log.info(":>>> 开始文件上传:{}", file == null ? null : file.getName());
        boolean checkName = fileUploadService.checkFileName(file.getOriginalFilename());
        if (!checkName) {
            log.error(":>>> 文件格式非法");
            return QResult.fail("文件格式非法");
        }
        if (file == null) {
            log.error(":>>> 上传文件为空");
            return QResult.fail("请上传文件。");
        }
        try {
            if (qqService.hasIllegal(file.getResource())) {
                log.error(":>>> 图片违规请重新上传。");
                return QResult.fail("图片违规请重新上传。");
            }
            String fileUrl = fileUploadService.uploadFile(file);
            QiniuFileRecord record = new QiniuFileRecord();
            record.setUrl(fileUrl);
            record.setAppId("Web-0001");
            record.setFileName(file.getOriginalFilename());
            String ip = IpUtils.getIpAddr(request);
            record.setIp(ip);
            qiniuFileRecordService.create(record);
            return QResult.success(fileUrl);
        } catch (BusinessException e) {
            log.error("<<<<<<<<<<<上传文件失败", e);
            return QResult.fail("上传文件失败");
        } catch (Exception e) {
            log.error("<<<<<<<<<<<上传文件失败", e);
            return QResult.fail("上传文件失败");
        }
    }

//    @PostMapping(value = "/public/upload-file/not-check")
//    @ApiOperation(value = "文件上传上传")
//    public QResult updateFileNotCheck(MultipartFile file) {
//        try {
//            boolean checkName = fileUploadService.checkFileName(file.getName());
//            if (!checkName) {
//                log.error(":>>> 文件格式非法");
//                return QResult.fail("文件格式非法");
//            }
//            return QResult.success(fileUploadService.uploadFile(file));
//        } catch (BusinessException e) {
//            log.error("<<<<<<<<<<<上传文件失败", e);
//            return QResult.fail("上传文件失败");
//        } catch (Exception e) {
//            log.error("<<<<<<<<<<<上传文件失败", e);
//            return QResult.fail("上传文件失败");
//        }
//    }

    @PostMapping(value = "/public/task/upload-file")
    public QResult updateTaskFileNotCheck(@RequestParam("file") MultipartFile file
            , HttpServletRequest request) {
        boolean checkName = fileUploadService.checkFileName(file.getOriginalFilename());
        if (!checkName) {
            log.error(":>>> 文件格式非法");
            return QResult.fail("文件格式非法");
        }
        long size = file.getSize();
        if (size == 0) {
            throw new BusinessException("图片未上传");
        }
        //如果文件大于10M
        if ((size / 1024) > 10240) {//kb
            throw new BusinessException("文件大小请勿超过10M");
        }
        try {
            String fileName = file.getOriginalFilename();
            String fileExt = fileName.substring(fileName.lastIndexOf("."));
            String uploadFileUrl = fileUploadService.uploadFile(file);
            Map<String, Object> result = new HashMap<>(3);
            result.put("url", uploadFileUrl);
            result.put("fileName", fileName);
            if (fileExt.equalsIgnoreCase(".jpeg") || fileExt.equalsIgnoreCase(".jpg") || fileExt.equalsIgnoreCase(".png") || fileExt.equalsIgnoreCase(".gif")) {
                result.put("fileType", 1);
            } else if (fileExt.equalsIgnoreCase(".pdf") || fileExt.equalsIgnoreCase(".dps") || fileExt.equalsIgnoreCase(
                    ".et") || fileExt.equalsIgnoreCase(
                    ".wps") || fileExt.equalsIgnoreCase(
                    ".pptx") || fileExt.equalsIgnoreCase(
                    ".ppt") || fileExt.equalsIgnoreCase(".docx") || fileExt.equalsIgnoreCase(".doc") || fileExt.equalsIgnoreCase(
                    ".txt") || fileExt.equalsIgnoreCase(
                    ".xlsx") || fileExt.equalsIgnoreCase(
                    ".xls")) {
                result.put("fileType", 2);
            } else {
                result.put("fileType", 3);
            }
            QiniuFileRecord record = new QiniuFileRecord();
            record.setUrl(uploadFileUrl);
            record.setAppId("Web-0001");
            record.setFileName(file.getOriginalFilename());
            String ip = IpUtils.getIpAddr(request);
            record.setIp(ip);
            qiniuFileRecordService.create(record);
            return QResult.success(result);
        } catch (BusinessException e) {
            log.error("<<<<<<<<<<<上传文件失败", e);
            return QResult.fail("上传文件失败");
        } catch (Exception e) {
            log.error("<<<<<<<<<<<上传文件失败", e);
            return QResult.fail("上传文件失败");
        }
    }
}
